<template>
    <div :id="'p'+times" ref="canvas" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
import * as echarts from 'echarts/core';
import { TooltipComponent, type TooltipComponentOption,
    LegendComponent, type LegendComponentOption,
    GridComponent, type GridComponentOption } from 'echarts/components';
import { PieChart, type PieSeriesOption } from 'echarts/charts';
import { LabelLayout } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([
  TooltipComponent,
  LegendComponent,
  PieChart,
  CanvasRenderer,
  LabelLayout,
  GridComponent
]);
type EChartsOption = echarts.ComposeOption<
  GridComponentOption | PieSeriesOption | LegendComponentOption | TooltipComponentOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})
let canHeight = computed(() => { return props.canvasHeight })
const times = new Date().getTime()
onMounted(() => {
    var chartDom = document.getElementById('p'+times);
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    option = {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            show: false,
            top: '5%',
            left: 'center'
        },
        grid: {
            top: '20%',
            left: 10,
            right: 10,
            bottom: 5,
            containLabel: true
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['30%', '60%'],
                avoidLabelOverlap: false,
                padAngle: 5,
                itemStyle: {
                    borderRadius: 10
                },
                label: {
                    show: true,
                    position: 'inside',
                    color: '#fff',
                    fontSize: 14,
                    fontWeight: 500,
                    overflow: 'breakAll',
                    formatter: '{b}: {c}/单位'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 16,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: props.data.seriesDataList
            }
        ]
    };

    option && myChart.setOption(option);
})
</script>

<style scope>
</style>
